package com.lzq.springbook.controller;

import com.lzq.springbook.model.*;
import com.lzq.springbook.service.BookService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

import static com.lzq.springbook.constant.Constant.SESSION_USER_KEY;

@Slf4j
@RequestMapping("/book")
@RestController
public class BookController {
    @Autowired
    private BookService bookService;
    @RequestMapping("/addBook")
    public Result addBook(BookInfo bookInfo) {
        log.info("addBook,bookInfo: {}",bookInfo);
        if(!StringUtils.hasLength(bookInfo.getBookName())
            || !StringUtils.hasLength(bookInfo.getAuthor())
            || bookInfo.getCount()==null
            || bookInfo.getPrice()==null
            || !StringUtils.hasLength(bookInfo.getPublish())
            || bookInfo.getStatus()==null
        ){
            return Result.fail("输入的参数不合法,请检查参数");
        }
        try{
            bookService.addBook(bookInfo);
            return Result.success("");
        }catch (Exception e) {
            log.error("添加图书失败");
            return Result.fail(e.getMessage());
        }
    }

    @RequestMapping("/getListByPage")
    public Result<BookInfo> getListByPage(PageRequest pageRequest, HttpSession session) {
        log.info("getListByPage,pageRequest: {}",pageRequest);
//        if(session.getAttribute(SESSION_USER_KEY)==null){
//            return Result.unLogin();
//        }
//        UserInfo userInfo = (UserInfo)session.getAttribute(SESSION_USER_KEY);
//        if(userInfo==null || userInfo.getId()<0 || "".equals(userInfo.getUserName())) {
//            return Result.unLogin();
//        }
        PageResult<BookInfo> result = bookService.getBookListByPage(pageRequest);
        return Result.success(result);
    }

    @RequestMapping("/getBookInfo")
    public BookInfo getBookInfo(Integer bookId) {
        log.info("getBookInfo,bookId: {}",bookId);
        return bookService.getBookInfo(bookId);
    }

    @RequestMapping("/updateBook")
    public Result updateBook(BookInfo bookInfo) {
        log.info("updateBook,bookId: {}",bookInfo);
        try{
            Integer result = bookService.updateBook(bookInfo);
            if(result>0) {
                return Result.success("");
            }
            return Result.fail("更新图书失败");
        }catch (Exception e) {
            log.error("更新图书失败");
            return Result.fail("更新图书失败");
        }
    }

    @RequestMapping("/batchDelete")
    public Boolean batchDelete(@RequestParam List<Integer> ids) {
        log.info("batchDelete,ids:{}",ids);
        try {
            bookService.batchDelete(ids);
        }catch (Exception e) {
            log.error("批量删除失败!");
            return false;
        }
        return true;
    }
}
